package com.tcoiss.log.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tcoiss.common.core.utils.poi.ExcelUtil;
import com.tcoiss.common.core.web.controller.BaseController;
import com.tcoiss.common.core.web.domain.AjaxResult;
import com.tcoiss.common.core.web.page.TableDataInfo;
import com.tcoiss.common.log.annotation.Log;
import com.tcoiss.common.log.enums.BusinessType;
import com.tcoiss.common.security.annotation.PreAuthorize;
import com.tcoiss.log.domain.OperLog;
import com.tcoiss.log.service.IOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 操作日志记录
 * 
 * @author tcoiss
 */
@RestController
@RequestMapping("/operlog")
public class OperlogController extends BaseController
{
    @Autowired
    private IOperLogService operLogService;

    @PreAuthorize(hasPermi = "log:operlog:list")
    @GetMapping("/list")
    public TableDataInfo list(OperLog operLog)
    {
        startPage();
        List<OperLog> list = operLogService.selectOperLogList(operLog);
        return getDataTable(list);
    }

    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @PreAuthorize(hasPermi = "log:operlog:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, OperLog operLog) throws IOException
    {
        List<OperLog> list = operLogService.selectOperLogList(operLog);
        ExcelUtil<OperLog> util = new ExcelUtil<OperLog>(OperLog.class);
        util.exportExcel(response, list, "操作日志");
    }

    @PreAuthorize(hasPermi = "log:operlog:remove")
    @DeleteMapping("/{operIds}")
    public AjaxResult remove(@PathVariable Long[] operIds)
    {
        return toAjax(operLogService.removeByIds(Arrays.asList(operIds)) ? 1 : 0);
    }

    @PreAuthorize(hasPermi = "log:operlog:remove")
    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public AjaxResult clean()
    {
        return toAjax(operLogService.remove(new QueryWrapper<>()) ? 1 : 0);
    }

    @PostMapping
    public AjaxResult add(@RequestBody OperLog operLog)
    {

        return toAjax(operLogService.save(operLog) ? 1 : 0);
    }
}
